package com.chen.positioning.util;

import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Service
public class LatitudeAndLongitudeUtil {
//    public final String GD_KEY = "61f70e4c4146cb3131cfca7493365692";
    public final String GD_KEY = "233cfceb92c7040af48169fc4c548188";
    private static final Logger logger = LoggerFactory.getLogger(LatitudeAndLongitudeUtil.class);

    /**
     * 根据城市或地址获取其经纬度
     *
     * @param city
     * @param address
     * @return
     * @throws Exception
     */
    public String httpURLConectionGET(String city, String address) throws Exception {
        StringBuffer result = new StringBuffer();
        String GET_URL = "https://restapi.amap.com/v3/geocode/geo?key=" + GD_KEY + "&address=" + address  + "&output=JSON";
        URL url = new URL(GET_URL); // 把字符串转换为URL请求地址
        logger.info("GET_URL :"+GET_URL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
        connection.connect();// 连接会话
        // 获取输入流
        String line;
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        logger.info("result ==" + result.toString());
        in.close();
        connection.disconnect();// 断开连接
        logger.info("result.toString() ====["+result.toString()+"]");
        return result.toString();
    }

    /**
     * 根据城市信息获取到其经度纬度
     *
     * @param city
     * @param address
     * @return
     * @throws Exception
     */
    public List getLatitudeAndLongitude(String city, String address) throws Exception {
        List<String> list = new ArrayList<String>();

        LatitudeAndLongitudeUtil latitudeAndLongitudeUtil = new LatitudeAndLongitudeUtil();
        String xmlInfo = latitudeAndLongitudeUtil.httpURLConectionGET(city, address);//根据城市或地址获取其经纬度


        JSONObject jsonObject = JSONObject.fromObject(xmlInfo);

        logger.info("getLatitudeAndLongitude - jsonObject===["+jsonObject.toString()+"]");
        String LatitudeAndLongitude = "";
        //判断输入的位置点是否存在
        if (jsonObject.getJSONArray("geocodes").size() > 0) {
            LatitudeAndLongitude = jsonObject.getJSONArray("geocodes").getJSONObject(0).get("location").toString();
        }
        if (StringUtils.isNotEmpty(LatitudeAndLongitude)) {
            logger.info("LatitudeAndLongitude ==" + LatitudeAndLongitude);
            String[] sz = LatitudeAndLongitude.split(",");
            for (String s : sz) {
                list.add(s);
            }
        } else {
            logger.info("LatitudeAndLongitude 為空");
        }
        logger.info("LatitudeAndLongitudeUtil - getLatitudeAndLongitude -list" + list.toString());
        return list;
    }

    /*public static void main(String[] args) {
        LatitudeAndLongitudeUtil latitudeAndLongitudeUtil = new LatitudeAndLongitudeUtil();
        try {
            List<String> list=latitudeAndLongitudeUtil.getLatitudeAndLongitude("","深圳");
            for (String l:list){
                logger.info("info :"+l);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
